import pandas as pd

# 读取 Excel
file_path = "C:\\Users\\Administrator\\Desktop\\ocr未保存到mongodb数据.xlsx"
df = pd.read_excel(file_path, sheet_name="Sheet1")

insert_statements = []

for _, row in df.iterrows():
    ucUserId = str(row["ucUserId"])
    createdAt = str(row["createdAt"])
    role = str(row["role"])
    imgUrl = str(row["imgUrl"])
    aiResult = str(row["aiResult"]).replace("'", '"')  # 替换为标准json格式

    contentMaps = {
        "imgUrl": imgUrl,
        "ocrResult": "",
        "role": role,
        "ucUserId": ucUserId,
        "aiResult": aiResult
    }

    stmt = f"""db.healthLog.insert({{
    "source": "ocrRecognition",
    "tag": "tagId_cure_log",
    "businessId": "{ucUserId}",
    "createdBy": "{ucUserId}",
    "createdById": "{ucUserId}",
    "createdAt": ISODate("{createdAt}"),
    "uploaderType": "{role}",
    "contentMaps": {contentMaps}
}});"""

    insert_statements.append(stmt)

# 保存到文件
with open("C:\\Users\\Administrator\\Desktop\\mongodb_inserts.js", "w", encoding="utf-8") as f:
    f.write("\n\n".join(insert_statements))

print(f"生成完成，共 {len(insert_statements)} 条记录")
